//
//  ClubTableViewCell.m
//  TasteEnjoy
//
//  Created by XuRui on 16/4/26.
//  Copyright © 2016年 XuRui. All rights reserved.
//

#import "ClubTableViewCell.h"

@interface ClubTableViewCell ()

@property (nonatomic, retain) BaseLabel *labelTitle; /**< 题目 */
@property (nonatomic, retain) BaseImageView *imgPhoto;  /**< 图片 */
@property (nonatomic, retain) BaseImageView *imgComment;  /**< 评论 */
@property (nonatomic, retain) BaseLabel *labelName; /**< 用户名 */
@property (nonatomic, retain) BaseLabel *labelComment; /**< 评论数 */
@property (nonatomic, retain) BaseLabel *labelTime; /**< 时间 */

@end

@implementation ClubTableViewCell


#pragma mark - ------------ 内存管理 ---------------
- (void)dealloc
{

    [_labelTitle release];
    [_imgPhoto release];
    [_imgComment release];
    [_labelName release];
    [_labelComment release];
    [_labelTime release];
    [_model release];
    [super dealloc];
    
}

#pragma mark - ------------ 初始化 ---------------
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        [self createSubViews];
    }
    return self;
    
}


#pragma mark - ------------ 创建子控件 ---------------
- (void)createSubViews
{
    
    //评论图标
    self.imgComment = [[BaseImageView alloc]init];
    [self.contentView addSubview:_imgComment];
    _imgComment.image = [UIImage imageNamed:@"pinglun"];
    
    //评论
    self.labelComment = [[BaseLabel alloc]init];
    [self.imgComment addSubview:_labelComment];
    _labelComment.textAlignment = NSTextAlignmentCenter;
    _labelComment.textColor = [UIColor grayColor];
    _labelComment.font = [UIFont systemFontOfSize:13];
    
    //题目
    self.labelTitle = [[BaseLabel alloc]init];
    [self.contentView addSubview:_labelTitle];
    _labelTitle.numberOfLines = 0;
    _labelTitle.font = [UIFont systemFontOfSize:15];
/** 夜间 */
    _labelTitle.dk_textColorPicker = DKColorWithColors([UIColor grayColor],[UIColor lightGrayColor]);
    
    //图片
    self.imgPhoto = [[BaseImageView alloc]init];
    [self.contentView addSubview:_imgPhoto];
    _imgPhoto.contentMode = UIViewContentModeScaleAspectFill; /**< 按比例填充 */
    _imgPhoto.clipsToBounds = YES;

    //名字 + club
    self.labelName = [[BaseLabel alloc]init];
    [self.contentView addSubview:_labelName];
    _labelName.textColor = [UIColor lightGrayColor];
    _labelName.font = [UIFont systemFontOfSize:13];
    
    //时间
    self.labelTime = [[BaseLabel alloc]init];
    [self.contentView addSubview:_labelTime];
    _labelTime.textColor = [UIColor lightGrayColor];
    _labelTime.font = [UIFont systemFontOfSize:13];
    _labelTime.textAlignment = NSTextAlignmentRight;
   
    [_labelTitle release];
    [_imgPhoto release];
    [_imgComment release];
    [_labelName release];
    [_labelComment release];
    [_labelTime release];

  
//    _imgPhoto.backgroundColor = [UIColor redColor];
//   _labelTime.backgroundColor = [UIColor yellowColor];
//    _labelComment.backgroundColor = [UIColor orangeColor];
//   _labelName.backgroundColor = [UIColor redColor];
//    _labelTitle.backgroundColor = [UIColor yellowColor];
    
}

#pragma mark - ------------ 约束大小 ---------------
- (void)layoutSubviews
{
    [super layoutSubviews];
    
    _imgComment.translatesAutoresizingMaskIntoConstraints = NO;
    _labelTitle.translatesAutoresizingMaskIntoConstraints = NO;
    _imgPhoto.translatesAutoresizingMaskIntoConstraints = NO;
    _labelName.translatesAutoresizingMaskIntoConstraints = NO;
    _labelTime.translatesAutoresizingMaskIntoConstraints = NO;
    
    
    /** 评论图标 */
    [_imgComment mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.mas_equalTo(self.contentView).offset(10);
        make.top.mas_equalTo(self.contentView).offset(5);
        make.width.mas_equalTo(36);
        make.height.mas_equalTo(38);
    }];
    
    [self layoutIfNeeded];  /**< 更新 要不 评论数 值 不上去 */
    /** 评论数 = 图片大小 */
    _labelComment.frame = CGRectMake(0, -3, _imgComment.bounds.size.width, _imgComment.bounds.size.height);
   
    
#pragma mark - ------------ 以下 约束 需清空之前约束 ---------------
    /** 标题 自适应 必须更新约束  */
    
    CGFloat h1 = [[self class]heightWithText:_labelTitle.text];
    [_labelTitle mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.top.mas_equalTo(self.contentView).offset(8);
        make.left.mas_equalTo(_imgComment.mas_right).offset(10);
        make.height.mas_equalTo(h1);
        make.right.mas_equalTo(self.contentView).offset(-10);
    }];

    
    /**< 如果有图片数据 就是这个高度 */
    if (_model.path != nil) {
        
        /** 图片 */
        _imgPhoto.hidden = NO;
        [_imgPhoto mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(_labelTitle.mas_bottom).offset(10);
            make.left.mas_equalTo(self.contentView).offset(55);
            make.width.mas_equalTo(100);
            make.height.mas_equalTo(120);
      
        }];
        
        
        /** 用户名 . club名 */
        [_labelName mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(_imgPhoto);
            make.top.mas_equalTo(_imgPhoto.mas_bottom).offset(10);
            make.right.mas_equalTo(_labelTime.mas_left).offset(-10);
            make.height.mas_equalTo(30);

        }];
        
        
        /** 时间 转换格式 */
        [_labelTime mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(_labelName);
            make.right.mas_equalTo(self.contentView).offset(-10);
            make.height.mas_equalTo(30);
            make.width.mas_equalTo(100);

        }];
    }
    
    else{

        /** 图片 */
//        [_imgPhoto mas_remakeConstraints:^(MASConstraintMaker *make) {
//            
//            make.top.mas_equalTo(_labelTitle.mas_bottom).offset(0);
//            make.left.mas_equalTo(self.contentView).offset(0);
//            make.width.mas_equalTo(0);
//            make.height.mas_equalTo(0);
//        }];

        _imgPhoto.hidden = YES;
        
        /** 用户名 . club名 */
        [_labelName mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.contentView).offset(55);
            make.top.mas_equalTo(_labelTitle.mas_bottom).offset(10);
            make.right.mas_equalTo(_labelTime.mas_left).offset(-10);
            make.height.mas_equalTo(30);
            
        }];
        
        /** 时间 转换格式 */
        [_labelTime mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(_labelTitle.mas_bottom).offset(10);
            make.right.mas_equalTo(self.contentView).offset(-15);
            make.height.mas_equalTo(30);
            make.width.mas_equalTo(60);
            
        }];

    }
    
}

#pragma mark - ------------ 赋值 ---------------
- (void)setModel:(ClubModel *)model
{

    if (_model != model) {
        [_model release];
        _model = [model retain];
    }
    
    _labelComment.text = model.comment_count.stringValue;
    
    if (_labelComment.text.integerValue > 1000) {
        /**< 判断如果 > 1000 显示几k */
        _labelComment.text = [NSString stringWithFormat:@"%ldk", _labelComment.text.integerValue / 1000];
    }
    
    
    _labelTitle.text = model.content;  /**< 此时偶尔控制台会出不明东西 数据有乱码表情 */

    _labelName.text = [[model.username stringByAppendingString:@" . "] stringByAppendingString:model.clubname];
    
   /** 时间戳转换 */
    _labelTime.text = [[self class]howlongToCurrntTime:model.active_time.stringValue];
    
    if (model.path!= nil) {
        
        [_imgPhoto sd_setImageWithURL:[NSURL URLWithString:model.path] placeholderImage:PLACEHOLDER];
    }
}


#pragma mark - ------------ 文本高度自适应 ---------------
+ (CGFloat)heightWithText:(NSString *)text
{

    
  CGRect rect = [text boundingRectWithSize:CGSizeMake(SCREEN_WIDTH - 65, 1000) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:15]} context:nil];
    
    return rect.size.height;
    
}


#pragma mark - ------------ 时间转换 ---------------

+ (NSString *)howlongToCurrntTime:(NSString *)time
{

    NSDate *date = [NSDate dateWithTimeIntervalSince1970:time.doubleValue];

    NSDateFormatter *dateFomatter = [[NSDateFormatter alloc]init];
    [dateFomatter setDateFormat:@"HH:mm"];
    
    //1970  转至今的时间
    NSString *fomatterString = [dateFomatter stringFromDate:date];
    
    [dateFomatter release];
    
    return fomatterString;
    

}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
